<?php
/**首页
 * Created by PhpStorm.
 * User: C。
 * Date: 2019/8/12
 * Time: 15:10
 */
namespace app\adminson\controller;
use app\adminson\controller\Base;
use think\Controller;
use think\Db;

/**
 * 首页展示控制器
 * Class Index
 * @package app\index\controller
 */
class Index extends Base
{
    /**
     * [index  后台首页数据]
     * @author [C。]
     * @return json
     */
    public function getIndex()
    {
        $howmany = input('get.d',6);
        $num = 1-$howmany ;
        $this->howmany = $howmany;

        $days = array();

        $mon1 = array();
        $mon2 = array();
        $psersonnum = array();

        $tmon1 = 0;
        $tmon2 = 0;

        for ($i = $num; $i < 1; $i++) {
            $ndaystime0 = $this::unixtime('day', $i);

            $ndaystime59 = $ndaystime0 + 3600 * 24 - 1; //最后一秒

            $days[] = date('Y-m-d', $ndaystime59);

            $cond = ['addtime' => ['between', $ndaystime0 . ',' . $ndaystime59]]; //, 'states' => 1
            $mon1a = 0 + Db::name('member')->where($cond)->count();

            $cond = ['states' => 1, 'paytime' => ['between', $ndaystime0 . ',' . $ndaystime59]];
            $mon2a = 0 + Db::name('recharge')->where($cond)->sum('amt');

            $personnuma = Db::name('recharge')->distinct(true)->field('memid')->where($cond)->count();

            $mon1[] = $mon1a;
            $mon2[] = $mon2a;
            $psersonnum [] = $personnuma;
            $tmon1 += $mon1a;

            $tmon2 += $mon2a;

        }
        $data = array();
        //时间轴
        $data['days'] = $days;
        //注册会员数
        $data['mon1'] = $mon1;
        //充值金额
        $data['mon2'] = $mon2;
        //募捐人数
        $data['psersonnum'] = $psersonnum;
        //当前时间段注册人数
        $data['tmon1'] = $tmon1;
        //当前时间段捐款总额
        $data['tmon2'] = $tmon2;
        //总用户数
        $data['zmon1'] = Db::name('member')->count();
        //总募捐数
        $data['zmon2'] = Db::name('recharge')->where('states',1)->sum('amt');

        //当前时间
        $today = strtotime(date('Y-m-d'));
        //七天前的时间
        $sday = strtotime(date('Y-m-d')) - 3600*24*7;
        $ssday = $sday + 3600*24;
        //获取之前第七天注册人数
        $con = ['addtime' => ['between', $sday . ',' . $ssday]];
        $mem = Db::name('member')->where($con)->field('id')->select();
        $mem = array_column($mem,'id');
        $zmem = count($mem);

        if($zmem){
            //次日留存率
            $time1 = $today - 3600*24*6;
            $num1 = Db::name('login_record')->where('logintime',$time1)->where('memid','in',$mem)->count();
            $data['rate1'] = number_format(($num1 / $zmem) * 100,0);
            //第三日留存率
            $time2 = $today - 3600*24*5;
            $num2 = Db::name('login_record')->where('logintime',$time2)->where('memid','in',$mem)->count();
            $data['rate2'] = number_format(($num2 / $zmem) * 100,0);
            //第四日留存率
            $time3 = $today - 3600*24*4;
            $num3 = Db::name('login_record')->where('logintime',$time3)->where('memid','in',$mem)->count();
            $data['rate3'] = number_format(($num3 / $zmem) * 100,0);
            //第五日留存率
            $time4 = $today - 3600*24*3;
            $num4 = Db::name('login_record')->where('logintime',$time4)->where('memid','in',$mem)->count();
            $data['rate4'] = number_format(($num4 / $zmem) * 100,0);
            //第六日留存率
            $time5 = $today - 3600*24*2;
            $num5 = Db::name('login_record')->where('logintime',$time5)->where('memid','in',$mem)->count();
            $data['rate5'] = number_format(($num5 / $zmem) * 100,0);
            //第七日留存率
            $time6 = $today - 3600*24;
            $num6 = Db::name('login_record')->where('logintime',$time6)->where('memid','in',$mem)->count();
            $data['rate6'] = number_format(($num6 / $zmem) * 100,0);
        }else{
            $data['rate1'] = 0;
            $data['rate2'] = 0;
            $data['rate3'] = 0;
            $data['rate4'] = 0;
            $data['rate5'] = 0;
            $data['rate6'] = 0;
        }
        return json_encode($data);
    }

    /**
     * 获取一个基于时间偏移的Unix时间戳
     *
     * @param string $type 时间类型，默认为day，可选minute,hour,day,week,month,quarter,year
     * @param int $offset 时间偏移量 默认为0，正数表示当前type之后，负数表示当前type之前
     * @param string $position 时间的开始或结束，默认为begin，可选前(begin,start,first,front)，end
     * @param int $year 基准年，默认为null，即以当前年为基准
     * @param int $month 基准月，默认为null，即以当前月为基准
     * @param int $day 基准天，默认为null，即以当前天为基准
     * @param int $hour 基准小时，默认为null，即以当前年小时基准
     * @param int $minute 基准分钟，默认为null，即以当前分钟为基准
     * @return int 处理后的Unix时间戳
     */
    public static function unixtime($type = 'day', $offset = 0, $position = 'begin', $year = null, $month = null, $day = null, $hour = null, $minute = null) {
        $year = is_null($year) ? date('Y') : $year;
        $month = is_null($month) ? date('m') : $month;
        $day = is_null($day) ? date('d') : $day;
        $hour = is_null($hour) ? date('H') : $hour;
        $minute = is_null($minute) ? date('i') : $minute;
        $position = in_array($position, array('begin', 'start', 'first', 'front'));
        switch ($type) {
            case 'minute':
                $time = $position ? mktime($hour, $minute + $offset, 0, $month, $day, $year) : mktime($hour, $minute + $offset, 59, $month, $day, $year);
                break;
            case 'hour':
                $time = $position ? mktime($hour + $offset, 0, 0, $month, $day, $year) : mktime($hour + $offset, 59, 59, $month, $day, $year);
                break;
            case 'day':
                $time = $position ? mktime(0, 0, 0, $month, $day + $offset, $year) : mktime(23, 59, 59, $month, $day + $offset, $year);
                break;
            case 'week':
                $time = $position ?
                    mktime(0, 0, 0, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 1 - 7 * (-$offset), $year) :
                    mktime(23, 59, 59, $month, $day - date("w", mktime(0, 0, 0, $month, $day, $year)) + 7 - 7 * (-$offset), $year);
                break;
            case 'month':
                $time = $position ? mktime(0, 0, 0, $month + $offset, 1, $year) : mktime(23, 59, 59, $month + $offset, get_month_days($month + $offset, $year), $year);
                break;
            case 'quarter':
                $time = $position ?
                    mktime(0, 0, 0, 1 + ((ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) - 1) * 3, 1, $year) :
                    mktime(23, 59, 59, (ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) * 3, get_month_days((ceil(date('n', mktime(0, 0, 0, $month, $day, $year)) / 3) + $offset) * 3, $year), $year);
                break;
            case 'year':
                $time = $position ? mktime(0, 0, 0, 1, 1, $year + $offset) : mktime(23, 59, 59, 12, 31, $year + $offset);
                break;
            default:
                $time = mktime($hour, $minute, 0, $month, $day, $year);
                break;
        }
        return $time;
    }
}
